﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace MouseAndCat.Model
{
    public static class MacVirtualKeyCodes
    {
        public static byte FromWindowsCode(byte windowsCode, out bool success)
        {
            success = true;
            Keys dotNetKey = (Keys)windowsCode;

            switch (dotNetKey)
            {
                case Keys.D0:
                    return (byte)Codes._0;
                case Keys.D1:
                    return (byte)Codes._1;
                case Keys.D2:
                    return (byte)Codes._2;
                case Keys.D3:
                    return (byte)Codes._3;
                case Keys.D4:
                    return (byte)Codes._4;
                case Keys.D5:
                    return (byte)Codes._5;
                case Keys.D6:
                    return (byte)Codes._6;
                case Keys.D7:
                    return (byte)Codes._7;
                case Keys.D8:
                    return (byte)Codes._8;
                case Keys.D9:
                    return (byte)Codes._9;
                case Keys.A:
                    return (byte)Codes.A;
                case Keys.B:
                    return (byte)Codes.B;
                case Keys.C:
                    return (byte)Codes.C;
                case Keys.D:
                    return (byte)Codes.D;
                case Keys.E:
                    return (byte)Codes.E;
                case Keys.F:
                    return (byte)Codes.F;
                case Keys.G:
                    return (byte)Codes.G;
                case Keys.H:
                    return (byte)Codes.H;
                case Keys.I:
                    return (byte)Codes.I;
                case Keys.J:
                    return (byte)Codes.J;
                case Keys.K:
                    return (byte)Codes.K;
                case Keys.L:
                    return (byte)Codes.L;
                case Keys.M:
                    return (byte)Codes.M;
                case Keys.N:
                    return (byte)Codes.N;
                case Keys.O:
                    return (byte)Codes.O;
                case Keys.P:
                    return (byte)Codes.P;
                case Keys.Q:
                    return (byte)Codes.Q;
                case Keys.R:
                    return (byte)Codes.R;
                case Keys.S:
                    return (byte)Codes.S;
                case Keys.T:
                    return (byte)Codes.T;
                case Keys.U:
                    return (byte)Codes.U;
                case Keys.V:
                    return (byte)Codes.V;
                case Keys.W:
                    return (byte)Codes.W;
                case Keys.X:
                    return (byte)Codes.X;
                case Keys.Y:
                    return (byte)Codes.Y;
                case Keys.Z:
                    return (byte)Codes.Z;
                case Keys.Down:
                    return (byte)Codes.DownArrow;
                case Keys.Right:
                    return(byte)Codes.RightArrow;
                case Keys.Up:
                    return(byte)Codes.UpArrow;
                case Keys.Left:
                    return(byte)Codes.LeftArrow;
                case Keys.Home:
                    return (byte)Codes.Home;
                case Keys.PageUp:
                    return (byte)Codes.PageUp;
                case Keys.PageDown:
                    return (byte)Codes.PageDown;
                case Keys.CapsLock:
                    return (byte)Codes.CapsLock;
                case Keys.End:
                    return (byte)Codes.End;
                case Keys.Enter:
                    return (byte)Codes.Return;
                case Keys.Escape:
                    return (byte)Codes.Escape;
                case Keys.LControlKey:
                    return (byte)Codes.Control;
                case Keys.LMenu:
                    return (byte)Codes.Option;
                case Keys.RMenu:
                    return (byte)Codes.RightOption;
                case Keys.RControlKey:
                    return (byte)Codes.RightControl;
                case Keys.LShiftKey:
                    return (byte)Codes.Shift;
                case Keys.RShiftKey:
                    return (byte)Codes.RightShift;
                case Keys.LWin:
                    return (byte)Codes.Command;
                case Keys.Tab:
                    return (byte)Codes.Tab;
                case Keys.Space:
                    return (byte)Codes.Space;
                case Keys.Back:
                    return (byte)Codes.Delete;
                case Keys.Delete:
                    return (byte)Codes.ForwardDelete;
                case Keys.OemBackslash:
                case Keys.OemPipe:
                    return (byte)Codes.Backslash;
                case Keys.Oemcomma:
                    return (byte)Codes.Comma;
                case Keys.OemPeriod:
                    return (byte)Codes.Period;
                case Keys.OemMinus:
                    return (byte)Codes.Minus;
                case Keys.Oemplus:
                    return (byte)Codes.Equal;
                case Keys.OemOpenBrackets:
                    return (byte)Codes.LeftBracket;
                case Keys.OemCloseBrackets:
                    return (byte)Codes.RightBracket;
                case Keys.OemSemicolon:
                    return (byte)Codes.Semicolon;
                case Keys.OemQuotes:
                    return (byte)Codes.Quote;
                case Keys.OemQuestion:
                    return (byte)Codes.Slash;
                case Keys.F6:
                    return (byte)Codes.F6;
                case Keys.F7:
                    return (byte)Codes.F7;
                case Keys.F8:
                    return (byte)Codes.F8;
                case Keys.F10:
                    return (byte)Codes.F10;
                case Keys.F11:
                    return (byte)Codes.F11;
                case Keys.F12:
                    return (byte)Codes.F12;

                default:
                    success = false;
                    System.Diagnostics.Debug.WriteLine("!!!!! The virtual key is unsupported: " + dotNetKey);
                    return 0xFF;
            }
        }
    }

    public enum Codes: byte
    {
        A = 0x00,
        S = 0x01,
        D = 0x02,
        F = 0x03,
        H = 0x04,
        G = 0x05,
        Z = 0x06,
        X = 0x07,
        C = 0x08,
        V = 0x09,
        B = 0x0B,
        Q = 0x0C,
        W = 0x0D,
        E = 0x0E,
        R = 0x0F,
        Y = 0x10,
        T = 0x11,
        _1 = 0x12,
        _2 = 0x13,
        _3 = 0x14,
        _4 = 0x15,
        _6 = 0x16,
        _5 = 0x17,
        Equal = 0x18,
        _9 = 0x19,
        _7 = 0x1A,
        Minus = 0x1B,
        _8 = 0x1C,
        _0 = 0x1D,
        RightBracket = 0x1E,
        O = 0x1F,
        U = 0x20,
        LeftBracket = 0x21,
        I = 0x22,
        P = 0x23,
        L = 0x25,
        J = 0x26,
        Quote = 0x27,
        K = 0x28,
        Semicolon = 0x29,
        Backslash = 0x2A,
        Comma = 0x2B,
        Slash = 0x2C,
        N = 0x2D,
        M = 0x2E,
        Period = 0x2F,
        Grave = 0x32,
        KeypadDecimal = 0x41,
        KeypadMultiply = 0x43,
        KeypadPlus = 0x45,
        KeypadClear = 0x47,
        KeypadDivide = 0x4B,
        KeypadEnter = 0x4C,
        KeypadMinus = 0x4E,
        KeypadEquals = 0x51,
        Keypad0 = 0x52,
        Keypad1 = 0x53,
        Keypad2 = 0x54,
        Keypad3 = 0x55,
        Keypad4 = 0x56,
        Keypad5 = 0x57,
        Keypad6 = 0x58,
        Keypad7 = 0x59,
        Keypad8 = 0x5B,
        Keypad9 = 0x5C,
        Return = 0x24,
        Tab = 0x30,
        Space = 0x31,
        Delete = 0x33,
        Escape = 0x35,
        Command = 0x37,
        Shift = 0x38,
        CapsLock = 0x39,
        Option = 0x3A,
        Control = 0x3B,
        RightShift = 0x3C,
        RightOption = 0x3D,
        RightControl = 0x3E,
        Function = 0x3F,
        F17 = 0x40,
        VolumeUp = 0x48,
        VolumeDown = 0x49,
        Mute = 0x4A,
        F18 = 0x4F,
        F19 = 0x50,
        F20 = 0x5A,
        F5 = 0x60,
        F6 = 0x61,
        F7 = 0x62,
        F3 = 0x63,
        F8 = 0x64,
        F9 = 0x65,
        F11 = 0x67,
        F13 = 0x69,
        F16 = 0x6A,
        F14 = 0x6B,
        F10 = 0x6D,
        F12 = 0x6F,
        F15 = 0x71,
        Help = 0x72,
        Home = 0x73,
        PageUp = 0x74,
        ForwardDelete = 0x75,
        F4 = 0x76,
        End = 0x77,
        F2 = 0x78,
        PageDown = 0x79,
        F1 = 0x7A,
        LeftArrow = 0x7B,
        RightArrow = 0x7C,
        DownArrow = 0x7D,
        UpArrow = 0x7E
    }
}